package com.share.advance.changeSet;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

/**
 * 更改集：包含了一个资源（rules）列表
 *       它也可以指向另一个更改集
 */
public class DroolsTest {
	
	private static  KnowledgeBuilder kbuilder = null;

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
        
            ksession.fireAllRules();
            ksession.dispose();
            logger.close();
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
    	if(null == kbuilder) {
    	       kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    	        kbuilder.add(ResourceFactory.newClassPathResource("changeset.xml", DroolsTest.class), ResourceType.CHANGE_SET);
    	        KnowledgeBuilderErrors errors = kbuilder.getErrors();
    	        if (errors.size() > 0) {
    	            for (KnowledgeBuilderError error: errors) {
    	                System.err.println(error);
    	            }
    	            throw new IllegalArgumentException("Could not parse knowledge.");
    	        }

    	}
    	KnowledgeBase  kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }

    public static class Message {

        public static final int HELLO = 0;
        public static final int GOODBYE = 1;

        private String message;

        private int status;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public int getStatus() {
            return this.status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

    }

}
